package org.catspaw.cherubim.security.rbac.persistence.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.catspaw.cherubim.persistence.ResourceCallback;
import org.catspaw.cherubim.persistence.jpa.EntityManagerMaster;
import org.catspaw.cherubim.security.rbac.Resource;
import org.catspaw.cherubim.security.rbac.User;
import org.catspaw.cherubim.security.rbac.persistence.RbacRepository;

public class JpaRbacRepository implements RbacRepository {

	private EntityManagerMaster	entityManagerMaster;

	public JpaRbacRepository(EntityManagerMaster entityManagerMaster) {
		this.entityManagerMaster = entityManagerMaster;
	}

	public List<String> findOperationCodesByResourceCode(final String resourceCode) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, List<String>>() {

			public List<String> doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findOperationCodesByResourceCode");
				query.setParameter(1, resourceCode);
				return query.getResultList();
			}
		});
	}

	public List<String> findResourceCodesByRoleCode(final String roleCode) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, List<String>>() {

			public List<String> doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findResourceCodesByRoleCode");
				query.setParameter(1, roleCode);
				return query.getResultList();
			};
		});
	}

	public List<String> findResourceCodesByUsername(final String username) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, List<String>>() {

			public List<String> doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findResourceCodesByUsername");
				query.setParameter(1, username);
				return query.getResultList();
			};
		});
	}

	public List<String> findRoleCodesByResourceCode(final String resourceCode) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, List<String>>() {

			public List<String> doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findRoleCodesByResourceCode");
				query.setParameter(1, resourceCode);
				return query.getResultList();
			};
		});
	}

	public List<String> findRoleCodesByUsername(final String username) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, List<String>>() {

			public List<String> doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findRoleCodesByUsername");
				query.setParameter(1, username);
				return query.getResultList();
			};
		});
	}

	public User findUserByUsername(final String username) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, User>() {

			public User doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findUserByUsername");
				query.setParameter(1, username);
				return (User) query.getSingleResult();
			}
		});
	}

	public String findResourceCodesByResourceString(final String resourceString) {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, String>() {

			public String doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findResourceCodesByResourceString");
				query.setParameter(1, resourceString);
				return (String) query.getSingleResult();
			}
		});
	}

	public List<? extends Resource> findAllResources() {
		return entityManagerMaster.execute(new ResourceCallback<EntityManager, List<? extends Resource>>() {

			public List<? extends Resource> doInResource(EntityManager em) {
				Query query = em.createNamedQuery("findAllResources");
				return query.getResultList();
			};
		});
	}
	
	public EntityManagerMaster getEntityManagerMaster() {
		return entityManagerMaster;
	}
}
